using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace IQIGame.Onigao.GamePlay
{
    /// <summary>
    /// 剧情使用的背景按钮
    /// </summary>
    public class ExButtonStory : Selectable, IPointerClickHandler, IBeginDragHandler, IDragHandler
    {
        [Header("2、4、8、16倍速出现时间")]
        public float pressDurationTime = 1.5f;
        public bool responseOnceByPress = true;
        public float pressRepeatInterval = 0.5f;
        public UnityEvent onClick;
        public UnityEvent onPress;
        public UnityEvent onPressUp;
        //区别于onPress 按下立即生效
        public UnityEvent onPressDown;
        public UnityEvent OnFirstExcute;
        public UnityEvent OnDragEvent;
        //鼠标或手指点击的位置是在屏幕左边还是右边,0代表左边,1代表右边
        private int MouseDir = -1;
        [Header("滑动距离比值")]
        public float SliderInterval = 43;
        [Header("进度条出现时间")]
        public float ProgressAppearTime = 0.5f;

        //拖动方向，0,1分别代表向上拖动和向下拖动
        private int SlideDir = -1;
        //滑动距离
        [HideInInspector]
        public float Sliderdis;
        private bool isDown = false;
        private bool isPress = false;
        private float downTime = 0;
        private float calctime = 0;
        private float pressedTime = 0;//按住的时间
        private int LastSlideDir;
        private float lastposy;
        private float nowposy;
        private float orginposy;
        //一开始按下得位置设置为这个，然后每当往上或往下滑与之前的滑动,呈相反状态则进行设置改变
        private float turnposy;
        public bool closePressAction = false;
        void Update()
        {
            if (isDown && !closePressAction)
            {
                if (!isPress)
                {
                    downTime += Time.unscaledDeltaTime;
                    if (downTime >= ProgressAppearTime)
                    {
                        calctime += Time.unscaledDeltaTime;
                        OnFirstExcute?.Invoke();
                    }
                    if (downTime >= pressDurationTime)
                    {
                        isPress = true;
                        onPress.Invoke();
                        downTime = 0;
                        calctime = 0;
                    }
                }
                else if (!responseOnceByPress)
                {
                    pressedTime += Time.unscaledDeltaTime;
                    downTime += Time.unscaledDeltaTime;
                    if (downTime >= pressRepeatInterval)
                    {
                        onPress.Invoke();
                        downTime = 0;
                        calctime = 0;
                    }
                }
            }
        }

        public float GetCalcTime()
        {
            return calctime;
        }
        /// <summary>
        /// 获取按了多久的信息
        /// </summary>
        public float GetPressedTime()
        {
            return pressedTime;
        }
        /// <summary>
        /// 获取downTime，用于avg长按加速播放
        /// </summary>
        public float GetDownTime()
        {
            return downTime;
        }
        /// <summary>
        /// 获取鼠标或手指点击的屏幕方向
        /// </summary>
        /// <returns></returns>
        public int GetMouseDir()
        {
            return MouseDir;
        }

        public override void OnPointerDown(PointerEventData eventData)
        {
            if (eventData.button == PointerEventData.InputButton.Left)
            {
                if (IsActive() && IsInteractable())
                {
                    float x = eventData.position.x;
                    if (x >= 0 && x < Screen.width / 2)
                    {
                        MouseDir = 0;
                    }
                    else if (x >= Screen.width / 2 && x < Screen.width)
                    {
                        MouseDir = 1;
                    }
                    onPressDown?.Invoke();
                    isDown = true;
                    pressedTime = 0;
                    downTime = 0;
                    calctime = 0;
                }
                base.OnPointerDown(eventData);
            }
        }

        public override void OnPointerUp(PointerEventData eventData)
        {
            if (eventData.button == PointerEventData.InputButton.Left)
            {
                calctime = 0;
                isDown = false;
                onPressUp.Invoke();
                base.OnPointerUp(eventData);
            }
        }

        public override void OnPointerExit(PointerEventData eventData)
        {
            if (eventData.button == PointerEventData.InputButton.Left)
            {
                isDown = false;
                if (isPress)
                {
                    onPressUp.Invoke();
                }
                isPress = false;
                base.OnPointerExit(eventData);
            }
        }

        public void OnPointerClick(PointerEventData eventData)
        {
            if (eventData.button == PointerEventData.InputButton.Left)
            {
                if (IsActive() && IsInteractable())
                {
                    if (!isPress)
                    {
                        onClick.Invoke();
                    }
                    else
                    {
                        isPress = false;
                    }
                }
            }
        }

        public void OnBeginDrag(PointerEventData eventData)
        {
            //记得屏蔽掉drag普通情况下
            turnposy = eventData.position.y;
            nowposy = turnposy;
            LastSlideDir = SlideDir;
        }

        public void OnDrag(PointerEventData eventData)
        {
            if (isPress)
            {
                lastposy = nowposy;
                nowposy = eventData.position.y;
                float dis = nowposy - lastposy;
                if (dis > 0)
                {
                    SlideDir = 0;
                }
                else if (dis < 0)
                {
                    SlideDir = 1;
                }

                if (LastSlideDir != SlideDir)
                {
                    turnposy = nowposy;
                    LastSlideDir = SlideDir;
                }

                Sliderdis = nowposy - turnposy;
                OnDragEvent?.Invoke();
            }
        }

        void OnApplicationFocus(bool hasFocus)
        {
            isDown = false;
            onPressUp?.Invoke();
            isPress = false;
        }

        protected override void OnDisable()
        {
            if (isPress)
            {
                onPressUp.Invoke();
            }
            isDown = false;
            isPress = false;
            pressedTime = 0;
            downTime = 0;
            calctime = 0;
        }
    }
}
